package com.lanou.wuyou.gift.category;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.CategoryTopBean;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/21.
 */

public class CateListTopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private CategoryTopBean bean;
    private Context context;

    public CateListTopAdapter(Context context) {
        this.context = context;
        Log.d("CateListTopAdapter", "CateListTopAdapter is created");
    }

    public void setBean(CategoryTopBean bean) {
        this.bean = bean;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        View view = LayoutInflater.from(context).inflate(R.layout.item_category_recycler_top,parent,false);
        viewHolder = new TopHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        TopHolder topHolder = (TopHolder) holder;
        if (bean != null){
            CategoryTopBean.DataBean.ColumnsBean columnsBean = bean.getData().getColumns().get(position);
            if (columnsBean != null){
                Picasso.with(context).load(columnsBean.getBanner_image_url()).into(topHolder.imageView);
                topHolder.author.setText(columnsBean.getAuthor());
                topHolder.subtitle.setText(columnsBean.getSubtitle());
                topHolder.title.setText(columnsBean.getTitle());
                Log.d("CateListTopAdapter", "1111"+columnsBean.getTitle());
            }
        }
    }

    @Override
    public int getItemCount() {
        if (bean == null){
            return 0;
        }
        if (bean.getData() == null){
            return 0;
        }
        return bean.getData().getColumns() != null && bean.getData().getColumns().size() > 0 ? bean.getData().getColumns().size() : 0;
    }
    class TopHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView subtitle,title,author;
        public TopHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.view_category_top);
            subtitle = (TextView) itemView.findViewById(R.id.subtitle_category_top);
            title = (TextView) itemView.findViewById(R.id.title_category_top);
            author = (TextView) itemView.findViewById(R.id.author_category_top);
        }
    }
}
